<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 9/6/2019
 * Time: 11:39 PM
 */

namespace p5\objects;


class FileLock
{
    private $path = '';
    //文件句柄
    private $fp = '';
    //锁文件
    private $lockFile = '';

    /**
     * 构造函数
     * @param string $path 锁的存放目录
     * @param string $name 锁 KEY
     */
    public function __construct($name, $path = '')
    {
        if (empty($path)) {
            $this->path = dirname(__FILE__) . '/';
        } else {
            $this->path = $path;
        }
        $this->lockFile = $this->path . $name . '.lock';
    }

    /**
     * 加锁
     */
    public function lock()
    {
        $this->fp = fopen($this->lockFile, 'a+');
        if ($this->fp === false) {
            return false;
        }
        //LOCK_EX 获取独占锁
        //LOCK_NB 无法建立锁定时，不阻塞
        return flock($this->fp, LOCK_EX | LOCK_NB);
    }

    /**
     * 解锁
     */
    public function unlock($removeFile = true)
    {
        try {
            if ($this->fp !== false) {
                @flock($this->fp, LOCK_UN);
                clearstatcache();
                @fclose($this->fp);
            }
            if ($removeFile) {
                @unlink($this->lockFile);
            }
        } catch (\Throwable $t) {
        }
    }

    public function releace()
    {
        $this->unlock();
    }

    public function releaceWithoutDeleteFile()
    {
        $this->unlock(false);
    }
}